import { SpriteFrame } from "cc"
import { UserInfo } from "../common/UserInfo"
import { EventType, buildingType, sAdvanced, sStudy, techDescList } from "../settings/const"
import ResManager from "../common/ResManager"
import { Building } from "./Buildings"
import { BManager } from "./BManager"
import { Events } from "./Events"
import { Soldier } from "./Soldier"

/**研发条件 */
export class Condition {
    /**
     * 类型
     */
    type: string
    /**
     * 等级
     */
    lv:number
    /**
     * id
     */
    id:number
    /**
     * 名称
     */
    name:string
    /**
     * 需要的建筑物
     */
    building:Building
    /**
     * 需要的科技
     */
    tech:Tech

    static create(obj:any):Condition {
        let con = new Condition()
        con.type = obj.type
        con.lv = obj.lv
        con.id = obj.id
        if(obj.type == 't') {
            con.tech = Tech.createById(obj.id)
            con.name = con.tech.name
        } else if(obj.type == 'b' || obj.type == 'bmax') {
            con.building = Building.createById(obj.id)
            con.name = con.building.name
        }
        
        return con
    }

    static createList(list:any[]):Condition[] {
        let conList = []
        for(let item of list) {
            conList.push(this.create(item))
        }

        return conList
    }

    get isEnough():boolean{
        let tb:any
        if(this.type == 't') {
            tb = Tech.findByIdAndLevel(this.id, this.lv)
        } else {
            tb = BManager.findByIdAndLevel(this.id, this.lv)
        }
       
        return !!tb
    }
}
/**科技类型 */
export enum TechType{
    Soldier = 'Soldier',
    Building = 'Building',
}


/**科技 */
export class Tech {
    id:number
    /**名称 */
    name:string
    /**种族 */
    race:number
    /**最大等级 */
    max:number
    /**当前等级 */
    level:number
    /**研发条件 */
    condition: Condition[]

    /**
     * 根据id 创建
     * @param id 
     * @returns 
     */
    static createById(id:number):Tech {
        let studyString = UserInfo.instance.villageData.studyString;
        let obj = techDescList[id];
        let tech = new Tech();
        tech.id = id;
        tech.name = obj.name;
        tech.race = obj.race;
        tech.max = obj.max;
        tech.level = studyString[id] ? studyString[id] : 0;
        tech.condition = Condition.createList(obj.condition);

        return tech;
    }

    /**
     * 获取科技列表
     * @returns 
     */
    static getList():Tech[] {
        let list: Tech[] = [];
        for(let id of Object.keys(techDescList)) {
            let tech = this.createById(parseInt(id));

            if(tech.race == 0 || tech.race == UserInfo.instance.race + 1) {
                list.push(tech);
            }
        }

        return list
    }

    /**
     * 根据id 和 等级查找
     * @param id 
     * @param level 
     * @returns 
     */
    static findByIdAndLevel(id:number, level:number):Tech{
        
        let list:Tech[] = this.getList()
        for(let item of list){
            if(item.id == id && item.level >= level){
                
                return item
            }
        }
        return null
    }

    /**
     * 根据id查找
     * @param id 
     * @returns 
     */
    static findById(id:number):Tech{
        let list:Tech[] = this.getList()
        for(let item of list){
            if(item.id == id){
                return item
            }
        }
        return null
    }

    /**
     * 获取科技头像
     * @returns 
     */
    async getTechImg():Promise<SpriteFrame>{

        try {
            let sf
            if(this.type == TechType.Soldier){
                sf = await ResManager.instance.loadAssetByUrl<SpriteFrame>(`soliderImg/${this.id}/spriteFrame`)
            } else {
                sf = await ResManager.instance.loadAssetByUrl<SpriteFrame>(`buildings/${this.id}/spriteFrame`)
            }

            
      
            return sf
        } catch (error) {
            
        }
    }

    /**获取科技研发事件的列表 */
    static getTechStudyEventList():Events[]{
        let list = []
        let eventList = UserInfo.instance.eventList
        
        for (let e of eventList) {
            if (e.eType == EventType.study) {
                list.push(e)
            }
        }
        return list
    }

    /**
     * 获取研发事件
     * @returns 
     */
    getStudyEvent():Events{
        let eventList = UserInfo.instance.eventList
        
        for (let e of eventList) {
            if (e.eType == EventType.study && e.eParam == this.id + '') {
                return e
            }
        }
        return null
    }

    /**研发时间 */
    get studyTime():number{
        let t =  (sStudy[this.id][5] / UserInfo.instance.speed) | 0;


        return t
    }

    /**升级时间 */
    get gradeTime():number{
        let t = (sAdvanced[this.id][this.level-1][5] / UserInfo.instance.speed) | 0;
        return t
    }

    /**
     * 获取研发需要的资源
     */
    get studyRes():{food:number, wood:number, mud:number, iron:number, time:number}{
        let s = sStudy[this.id]
        let res = {
            wood:s[1],
            mud:s[2],
            iron:s[3],
            food: s[4],
            time:s[5],
        }
        return res
    }

    /**建筑科技是否满足 */
    get isBuildingEnough():boolean{
        for(let con of this.condition){
            if(!con.isEnough){
                return false
            }
        }

        return true
    }

    /**资源是否满足 */
    get isResEnough():boolean{
        let villageData = UserInfo.instance.villageData
        let food = villageData.res.food_reserve
        let iron = villageData.res.iron_reserve
        let wood = villageData.res.wood_reserve
        let mud = villageData.res.mud_reserve

        if (food < this.studyRes.food) {
            return false
        }
        if (iron < this.studyRes.iron) {
            return false
        }
        if (wood < this.studyRes.wood) {
            return false
        }
        if (mud < this.studyRes.mud) {
            return false
        }
        return true
    }

    get isFoodEnough():boolean{
        let villageData = UserInfo.instance.villageData
        let food = villageData.res.food_reserve
        return food >= this.studyRes.food
    }

    get isWoodEnough():boolean{
        let villageData = UserInfo.instance.villageData
        let wood = villageData.res.wood_reserve
        return wood >= this.studyRes.wood
    }

    get isMudEnough():boolean{
        let villageData = UserInfo.instance.villageData
        let mud = villageData.res.mud_reserve
        return mud >= this.studyRes.mud
    }

    get isIronEnough():boolean{
        let villageData = UserInfo.instance.villageData
        let iron = villageData.res.iron_reserve
        return iron >= this.studyRes.iron
    }

    get type():TechType{
        if(this.id < 40){
            return TechType.Soldier
        } 

        if(this.id > 40){
            return TechType.Building
        }

       
    }

    /**建筑科技的描述 */
    get desc():string{
        let map = {
            41: buildingType.B05_Sawmill,
            42: buildingType.B06_Brickyard,
            43: buildingType.B07_Foundry,
            44: buildingType.B08_GrainMill,
            45: buildingType.B09_Bakery,
            46: buildingType.B28_TradeOffice,
        }

        if(this.type == TechType.Soldier){
            return Soldier.createById(this.id).desc
        } else {
            return Building.createById(map[this.id]).desc
        }

        
    }
}